package com.example.myview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import com.example.bean.CrossBean;
import com.example.bean.Mystyle;
import com.example.bean.Point;
import com.example.bean.StickData;
import com.example.utils.ColorUtil;
import com.example.utils.ColorUtil2;
import com.example.utils.DrawUtils;
import com.example.utils.GridUtils;
import com.example.utils.IndexParseUtil;
import com.example.utils.LineUtil;
import com.example.utils.NumberUtil2;

import java.util.ArrayList;



/**
 * Created by Arvin on 2016/10/25.
 * K线View
 */
public class KLineView extends ChartView implements ChartConstant {
    //烛形图加空白的宽度和烛形图宽度之比
    public static final float WIDTH_SCALE = 1.2f;
    //烛形图和右侧空白的宽度
    public float DEFUALT_WIDTH = 19;
    //K线所有数据
    private ArrayList<StickData> data;
    //K线展示的数据
    private ArrayList<StickData> showList;
    //一屏烛形图数量
    private int drawCount;
    //没两个烛形图x轴的距离
    private float candleXDistance;

    //当前画图偏移量（往右滑动之后）
    private int offset;
    //y轴最大值
    protected double yMax;
    //y轴最小值
    protected double yMin;

    protected double yUnit;
    protected float xUnit;
    /**
     * 价格精度 默认2
     */
    private int scale_price=2;
    MsgView.IcbCall icb;
    /**
     * 距离父布局的高度
     */
   public float heightToParent;
    /**
     * 是否正在画样式，如果在画样式，k线停止滑动
     */
   public  StyleView styleView;

    /**
     * 十字架移动时候的点
     */
   float x;
   float y;

    /**
     * 设置样式回调
     * @param icb
     */
    public void setStyleBack(MsgView.IcbCall icb)
    {
        styleView.setBack(icb);
        styleView.setkLineView(this);

    }

   public void setMaBack(MsgView.IcbCall icb)
   {
       this.icb=icb;
   }
    public KLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDrawingCacheEnabled(true);
        styleView=new StyleView(context);
    }

    @Override
    protected boolean onViewScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        if(data != null && drawCount < data.size() && Math.abs(distanceX) > DEFUALT_WIDTH) {
            int temp = offset + (int)(0 - distanceX / DEFUALT_WIDTH);
            if(temp < 0 || temp + drawCount > data.size()) {

            } else {
                offset = temp;
                postInvalidate();

            }
            return true;
        }
        return false;
    }

    public ScaleGestureDetector mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.SimpleOnScaleGestureListener(){
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            Log.e("test","4");
            if(crossView != null) {
                if (crossView.getVisibility() == View.VISIBLE) {
                    crossView.setVisibility(GONE);
                    onDismiss();
                }
            }

            if(data == null) return super.onScale(detector);
            //放大是由1变大，缩小是由1变小
            float scale = detector.getScaleFactor();
            //这个变化太快，把scale变慢一点
            scale = 1 + ((scale - 1) * 0.08f);
            drawCount = (int) (mWidth / DEFUALT_WIDTH);
            if(scale < 1 && drawCount >= data.size()) {
                //缩小时，当缩到一屏显示完时，则不再缩小
            } else if(scale > 1 && drawCount < 50) {
                //放大时，当一屏少于20个时，则不再放大
            } else {
                DEFUALT_WIDTH = DEFUALT_WIDTH * scale;
                invalidate();
            }
            return super.onScale(detector);
        }
    });




    private float lastX;
    private  float lastY;
    private  int lastXOffse;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("test1","1");
        //样式移动时使用
        if(showList!=null){
        if (event.getAction() == event.ACTION_DOWN)
        {
            lastX=event.getX();
            lastY=event.getY();
            int position_last = (int) Math.rint(new Double(event.getX()) / new Double(DEFUALT_WIDTH));
            if (position_last< showList.size()) {
                StickData sd = showList.get(position_last);
                lastXOffse= data.indexOf(sd);
            }
        }
           else if (event.getAction() == event.ACTION_UP) {
            if(styleView.icb!=null)
            styleView.icb.cancle(null);
        }
        }

            if (styleView.getState() == 1) {
                if (crossView == null || showList == null)
                    return true;
                int position = (int) Math.rint(new Double(event.getX()) / new Double(DEFUALT_WIDTH));
                if (position < showList.size()) {
                    StickData sd = showList.get(position);
                    int offset = data.indexOf(sd);
                    Point point = new Point();
                    //point.setX(event.getX());
                    //point.setY(event.getY());
                    if (lineType != 24) {

                        point.setTime(sd.getTimeAll());
                    } else {

                        point.setTime(sd.getTime1());
                    }
                    point.setOffsetX(offset);
                    point.setPrice(getPrice(event.getY()));
                    if (event.getAction() == event.ACTION_DOWN) {
                        styleView.addPoint(point);
                        invalidate();
                    }
                }
                return true;

            } else if (styleView.getState() == 2)
            {
                if (event.getAction() == event.ACTION_DOWN)
                {
                    Mystyle style= styleView.getOnMysyle(event.getX(), event.getY());
                    if(style!=null)
                    {
                        styleView.setSelectStyle(style);

                        if (style.getSelectState() == 1)

                        {

                            styleView.icb.call(splitYuan(event.getX(),event.getY()),event.getX(),event.getY());
                        }
                    }
                    else
                    {
                        styleView.setState(0);
                        invalidate();
                        return false;
                    }
                }
                if (event.getAction() == event.ACTION_MOVE)
                {
                    if (crossView == null || showList == null)
                        return true;


                    int position = (int) Math.rint(new Double(event.getX()) / new Double(DEFUALT_WIDTH));
                    if (position < showList.size()) {
                        StickData sd = showList.get(position);
                        int offset = data.indexOf(sd);
                        Point point = new Point();
                        //point.setX(event.getX());
                        //point.setY(event.getY());
                        if (lineType != 24) {

                            point.setTime(sd.getTimeAll());
                        } else {

                            point.setTime(sd.getTime1());
                        }

                        point.setOffsetX(offset);
                        point.setPrice(getPrice(event.getY()));
                        if (styleView.mystyle.getSelectState() == 1)

                        {
                            styleView.updateSelecStyle(point);
                            styleView.icb.call(splitYuan(event.getX(),event.getY()),event.getX(),event.getY());

                        }
                        else if(styleView.mystyle.getSelectState()==2)
                        {

                                countStyle(styleView.mystyle,offset-lastXOffse,event.getY()-lastY);
                                //横线或者竖线
                                if(styleView.mystyle.index==4||styleView.mystyle.index==3)
                                {
                                    styleView.icb.call(splitYuan(event.getX(),event.getY()),event.getX(),event.getY());
                                }
                        }

                        //更新上一个点的相关信息
                        lastX=event.getX();
                        lastY=event.getY();
                        lastXOffse=offset;

                    }
                }

                invalidate();
                return true;
            }
            else if (styleView.getState() == 0)
            {
                if (event.getAction() == event.ACTION_DOWN)
                {
                    Mystyle style= styleView.getOnMysyle(event.getX(), event.getY());
                    if(style!=null)
                    {
                        styleView.setSelectStyle(style);
                        invalidate();
                        return true;
                    }
                }

            }

       // Log.e("test","1");
        int pointerCount = event.getPointerCount();
        if(pointerCount==1)
        {

            if(crossView != null) {
                if (crossView.getVisibility() == View.VISIBLE) {
                    crossView.gestureDetector.onTouchEvent(event);
                    return true;
                }
            }
            if (gestureDetector != null)
                if(crossView!=null&&crossView.getVisibility()==VISIBLE) {
                    crossView.setKline(this);
                }
                else
                {
                    if(crossView!=null) {
                        crossView.setKline(null);
                    }
                }
                gestureDetector.onTouchEvent(event);

        }
        else
        {
            if(mScaleGestureDetector != null)
                mScaleGestureDetector.onTouchEvent(event);
        }


        return true;
    }

    /**
     * 移动整个样式时，计算样式的坐标
     * @param style
     * @param xOffset_size 相对于屏幕左侧，k线的偏移移动的大小
     * @param ysize y轴方向 移动的大小
     */
    public void countStyle(Mystyle style,int xOffset_size,float ysize)
    {
        for(int i=0;i<style.list.size();i++)
        {
            Point point=style.list.get(i);
            point.setOffsetX(point.getOffsetX()+xOffset_size);
            point.setPrice(getPrice(point.getY()+ysize));

        }
    }
    OnClickListener ocl;
    public void setSingOn(OnClickListener ocl)
    {
        this.ocl=ocl;
    }
    @Override
    public void singleOn(MotionEvent e) {
        if(ocl!=null)
        {

            if(heightToParent>mainH)
            {
                ocl.onClick(this);
            }
            else
            {
                ocl.onClick(null);
            }

        }


    }

    @Override
    protected void init() {
        if(data == null) return;
        drawCount = (int) (mWidth / DEFUALT_WIDTH);
        candleXDistance = drawCount * WIDTH_SCALE;
        if(data != null && data.size() > 0) {
            if(drawCount < data.size()) {
                getShowList(offset);
            } else {
                showList = new ArrayList<>();
                showList.addAll(data);
            }
        }
        if(showList == null) return;
        double[] low = new double[showList.size()];
        double[] high = new  double[showList.size()];
        int i = 0;
        for(StickData d : showList) {
            low[i] = d.getLow();
            high[i] =  d.getHigh();
            i++;
        }
        double[] maxAndMin = LineUtil.getMaxAndMin2(low, high);
        yMax = maxAndMin[0];
        yMin = maxAndMin[1];

        if(lineIndexType==kline_boll)
        {   int a=0;
            double[] up = new double[showList.size()];
            double[] down = new  double[showList.size()];
            for(StickData d : showList) {
                up[a] = d.getUp();
                down[a] =  d.getDn();
                if(up[a]<=0)
                {
                    up[a]=d.getOpen();
                }
                if(down[a]<=0)
                {
                    down[a]=d.getOpen();
                }
                a++;
            }
            double[] maxAndMin2 = LineUtil.getMaxAndMin2(low, high,up,down);
            yMax = maxAndMin2[0];
            yMin = maxAndMin2[1];
        }
        else if(lineIndexType==kline_MA)
        {
            int a=0;
            double[] up = new double[showList.size()];
            double[] down = new  double[showList.size()];
            for(StickData d : showList) {
                up[a] = d.getSma5();
                down[a] =  d.getSma30();
                if(up[a]<=0)
                {
                    up[a]=d.getOpen();
                }
                if(down[a]<=0)
                {
                    down[a]=d.getOpen();
                }
                a++;
            }
            double[] maxAndMin2 = LineUtil.getMaxAndMin3(low, high,up,down);
            yMax = maxAndMin2[0];
            yMin = maxAndMin2[1];

        }

        //将最大值 扩大10%  最小值 减小10以免和坐标轴重合
        double d1=yMax-yMin;
        yMax=yMax+d1*0.08;
        yMin=yMin-d1*0.08;




        yUnit =  (yMax - yMin) / (mainH);
        xUnit = mWidth / drawCount;
    }

    @Override
    protected void drawStyle(Canvas canvas)
    {
         if(showList!=null&&showList.size()>0)
         {

             styleView.drawStyle(canvas);
         }

    }

    @Override
    protected void drawGrid(Canvas canvas) {
        GridUtils.drawGrid(this,canvas, mWidth, mainH);
        GridUtils.drawIndexGrid(this,canvas, indexStartY-zhibiaoY, mWidth, indexH+zhibiaoY);
    }

    @Override
    protected void drawCandles(Canvas canvas) {
        if(data == null || data.size() == 0) return;
        float x = 0;
        if(showList == null || showList.size() == 0) return;
        //计算出页面能显示多少个
        for(int i = 0; i < showList.size(); i++) {
            if(drawCount < data.size()) {
                x = mWidth - (mWidth / drawCount * (showList.size() - i));
                if(x<0)
                {
                    x=0;
                }
            } else {
                x = (mWidth / drawCount *i);
            }
            int color=DrawUtils.getKlineColor(i,showList);
            DrawUtils.drawCandle(canvas,
                    parseNumber(showList.get(i).getHigh()),
                    parseNumber(showList.get(i).getLow()),
                    parseNumber(showList.get(i).getOpen()),
                    parseNumber(showList.get(i).getClose()),
                    x,
                    parseNumber(showList.get(i).getHigh()),
                    candleXDistance,
                    mWidth,color);
        }
    }
    public float getXZuoBiao(int offset)
    {
        float x=0;
        if(showList!=null&&showList.size()>0)
        {
            StickData sd=showList.get(0);
            int coffset=data.indexOf(sd);
            x=DEFUALT_WIDTH/2+(offset-coffset)*DEFUALT_WIDTH;
        }
        return x;
    }

    /**
     * eMA5 eMA10 eMA20
     * @param canvas
     */
    @Override
    protected void drawEMA(Canvas canvas) {
        if(data == null || data.size() == 0) return;
       // IndexParseUtil.initEma(showList);

        if(crossView.getVisibility()==GONE)
        {
           icb.call(showList.get(showList.size()-1),0,0);
        }

        float[] ema5 = new float[showList.size()];
        float[] ema10 = new float[showList.size()];
        float[] ema20 = new float[showList.size()];
        float[] ema30 = new float[showList.size()];
        int size = showList.size();
        for (int i = 0; i < showList.size(); i++) {
            //if(size > IndexParseUtil.START_SMA5)
                ema5[i] = (float) showList.get(i).getEma5();
            //if(size > IndexParseUtil.START_SMA10)
                ema10[i] = (float) showList.get(i).getEma10();
            //if(size > IndexParseUtil.START_SMA20)
                ema20[i] = (float) showList.get(i).getEma20();
            //if(size > IndexParseUtil.START_SMA30)
                ema30[i] = (float) showList.get(i).getEma30();
        }
        DrawUtils.drawLineWithXOffset2(canvas, ema5, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA5, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
        DrawUtils.drawLineWithXOffset2(canvas, ema10, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA10, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
        DrawUtils.drawLineWithXOffset2(canvas, ema20, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA20, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
        DrawUtils.drawLineWithXOffset2(canvas, ema30, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA30, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
    }
    /**
     * SMA5 SMA10 SMA20
     * @param canvas
     */
    @Override
    protected void drawLines(Canvas canvas) {
        if(data == null || data.size() == 0) return;
        //IndexParseUtil.initSma(showList);
        if(crossView.getVisibility()==GONE)
        {
            icb.call(showList.get(showList.size()-1),0,0);
        }
        float[] sma5 = new float[showList.size()];
        float[] sma10 = new float[showList.size()];
        float[] sma20 = new float[showList.size()];
        float[] sma30 = new float[showList.size()];
        int size = showList.size();
        for (int i = 0; i < showList.size(); i++) {
            //if(size > IndexParseUtil.START_SMA5)
                sma5[i] = (float) showList.get(i).getSma5();
            //if(size > IndexParseUtil.START_SMA10)
                sma10[i] = (float) showList.get(i).getSma10();
            //if(size > IndexParseUtil.START_SMA20)
                sma20[i] = (float) showList.get(i).getSma20();
            //if(size > IndexParseUtil.START_SMA30)
                sma30[i] = (float) showList.get(i).getSma30();
        }
        DrawUtils.drawLineWithXOffset2(canvas, sma5, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA5, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
        DrawUtils.drawLineWithXOffset2(canvas, sma10, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA10, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
        DrawUtils.drawLineWithXOffset2(canvas, sma20, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA20, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
        DrawUtils.drawLineWithXOffset2(canvas, sma30, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA30, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
    }
    @Override
    protected void drawBOLL(Canvas canvas) {
        if(crossView.getVisibility()==GONE)
        {
            icb.call(showList.get(showList.size()-1),0,0);
        }
        if(data == null || data.size() == 0) return;
        float[] up = new float[showList.size()];
        float[] mb = new float[showList.size()];
        float[] dn = new float[showList.size()];

        for (int i = 0; i < showList.size(); i++) {

            up[i] = (float) showList.get(i).getUp();

            mb[i] = (float) showList.get(i).getMb();
            dn[i] = (float) showList.get(i).getDn();

        }
       // float maxAndMin[] = LineUtil.getMaxAndMin(up,mb,dn);
        DrawUtils.drawLineWithXOffset2(canvas, up, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA10, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
        DrawUtils.drawLineWithXOffset2(canvas, mb, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA5, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
        DrawUtils.drawLineWithXOffset2(canvas, dn, DEFUALT_WIDTH, mainH, ColorUtil.COLOR_SMA30, (float) yMax, (float) yMin, DEFUALT_WIDTH / 2);
    }
   private void showMa()
   {

   }
    @Override
    protected void drawText(Canvas canvas) {
        if(data == null || data.size() == 0) return;
        String timestart=showList.get(0).getTime2();
        String timeend=showList.get(showList.size() - 1).getTime2();
        if(lineType==24)
        {
            timestart=showList.get(0).getTime1();
            timeend=showList.get(showList.size() - 1).getTime1();
        }
        //4，画X轴时间
        if(showList.size() <= drawCount) {
            DrawUtils.drawKLineXTime(this,canvas, timestart,  timeend, mWidth, mainH);
        } else {
            DrawUtils.drawKLineXTime(this,canvas, timestart, null, mWidth, mainH);
        }
        //5，画Y轴价格
        DrawUtils.drawKLineYPrice(this,canvas, yMax, yMin, mainH);

        //画提示指标
        if(crossView.getVisibility() == GONE)
        {
            drawZhiBiao(canvas);
        }
    }

    @Override
    protected void drawVOL(Canvas canvas) {
        if(data == null || data.size() == 0) return;


        double max = 0;
        for(StickData data : showList) {
            max = data.getCount() > max ? data.getCount() : max;
        }
        //如果量全为0，则不画
        if(max == 0) return;
        //2,画量线，多条竖直线
        DrawUtils.drawVOLRects(canvas, xUnit,indexStartY, indexH, max, showList);
        //3,画量的sma5,sma10
       // drawCountSma(canvas, (float) max);
    }

    @Override
    protected void drawZJ(Canvas canvas) {
        if(data == null || data.size() == 0) return;
        float[] sup = new float[showList.size()];
        float[] big = new float[showList.size()];
        float[] mid = new float[showList.size()];
        float[] sma = new float[showList.size()];
        int size = showList.size();
        for (int i = 0; i < showList.size(); i++) {
            sup[i] = (float) showList.get(i).getSp();
            big[i] = (float) showList.get(i).getBg();
            mid[i] = (float) showList.get(i).getMd();
            sma[i] = (float) showList.get(i).getSm();
        }
        float maxAndMin[] = LineUtil.getMaxAndMin(sup, big, mid, sma);
        DrawUtils.drawLines(canvas, sup, DEFUALT_WIDTH, indexH - 2, ColorUtil.COLOR_ZJ_SUPER, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawLines(canvas, big, DEFUALT_WIDTH, indexH - 2, ColorUtil.COLOR_ZJ_BIG, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawLines(canvas, mid, DEFUALT_WIDTH, indexH - 2, ColorUtil.COLOR_ZJ_MIDDLE, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawLines(canvas, sma, DEFUALT_WIDTH, indexH - 2, ColorUtil.COLOR_ZJ_SMALL, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawIndexMiddleText(canvas, (maxAndMin[0] - maxAndMin[1]) / 2 + "", indexStartY + indexH / 2);
    }

    @Override
    protected void drawMACD(Canvas canvas) {
        if(data == null || data.size() == 0) return;
        float[] dif = new float[showList.size()];
        float[] dea = new float[showList.size()];
        float[] macd = new float[showList.size()];
        for (int i = 0; i < showList.size(); i++) {
            //if(data.indexOf(showList.get(i)) > IndexParseUtil.START_DIF)
                dif[i] = (float) showList.get(i).getDif();
           // if(data.indexOf(showList.get(i)) > IndexParseUtil.START_DEA) {
                dea[i] = (float) showList.get(i).getDea();
                macd[i] = (float) showList.get(i).getMacd();
            //}
        }
        float maxAndMin[] = LineUtil.getMaxAndMin(dif, dea, macd);
        DrawUtils.drawMACDRects(canvas, macd, maxAndMin[0], maxAndMin[1], indexH - 4, indexStartY + 2, DEFUALT_WIDTH);
        DrawUtils.drawLines(canvas, dif, DEFUALT_WIDTH, indexH - 4, ColorUtil.COLOR_SMA5, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawLines(canvas, dea, DEFUALT_WIDTH, indexH - 4, ColorUtil.COLOR_SMA10, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        //DrawUtils.drawIndexMiddleText(canvas, "0", indexStartY + indexH / 2);
    }

    @Override
    protected void drawRSI(Canvas canvas) {
        if(data == null || data.size() == 0) return;
        float[] rsi6 = new float[showList.size()];
        float[] rsi12 = new float[showList.size()];
        float[] rsi24 = new float[showList.size()];

        for (int i = 0; i < showList.size(); i++) {

                rsi6[i] = (float) showList.get(i).getRsi6();


                rsi12[i] = (float) showList.get(i).getRsi12();


                rsi24[i] = (float) showList.get(i).getRsi24();







        }
        float maxAndMin[] = LineUtil.getMaxAndMin(rsi6,rsi12,rsi24);
        DrawUtils.drawLines2(canvas, rsi6, DEFUALT_WIDTH, indexH - 4, ColorUtil.COLOR_SMA5, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawLines2(canvas, rsi12, DEFUALT_WIDTH, indexH - 4,ColorUtil.COLOR_SMA10, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawLines2(canvas,rsi24, DEFUALT_WIDTH, indexH - 4, ColorUtil.COLOR_SMA30, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        //DrawUtils.drawIndexMiddleText(canvas, (maxAndMin[0] - maxAndMin[1]) / 2 + "", indexStartY + indexH / 2);
    }
    @Override
    protected void drawROC(Canvas canvas) {
        if(data == null || data.size() == 0) return;
        float[] rocn = new float[showList.size()];
        float[] rocm= new float[showList.size()];
        for (int i = 0; i < showList.size(); i++) {

            rocn[i] = (float) showList.get(i).getRocN();
            rocm[i] = (float) showList.get(i).getRocM();

        }
        float maxAndMin[] = LineUtil.getMaxAndMin(rocn,rocm);
        DrawUtils.drawLines2(canvas, rocn, DEFUALT_WIDTH, indexH - 4, ColorUtil.COLOR_SMA5, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawLines2(canvas, rocm, DEFUALT_WIDTH, indexH - 4, ColorUtil.COLOR_SMA10, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);

       // DrawUtils.drawIndexMiddleText(canvas, 0+ "", indexStartY + indexH / 2);
    }
    @Override
    protected void drawKDJ(Canvas canvas) {
        if(data == null || data.size() == 0) return;
        float[] kl = new float[showList.size()];
        float[] dl = new float[showList.size()];
        float[] jl = new float[showList.size()];
        int size = showList.size();
        for (int i = 0; i < showList.size(); i++) {

                kl[i] = (float) showList.get(i).getK();

                dl[i] = (float) showList.get(i).getD();
                jl[i] = (float) showList.get(i).getJ();

        }
        float maxAndMin[] = LineUtil.getMaxAndMin(kl, dl, jl);
        DrawUtils.drawLines(canvas, kl, DEFUALT_WIDTH, indexH - 4, ColorUtil.COLOR_SMA5, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawLines(canvas, dl, DEFUALT_WIDTH, indexH - 4, ColorUtil.COLOR_SMA10, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawLines(canvas, jl, DEFUALT_WIDTH, indexH - 4, ColorUtil.COLOR_SMA30, maxAndMin[0], maxAndMin[1], false, indexStartY + 2, DEFUALT_WIDTH / 2);
       // DrawUtils.drawIndexMiddleText(canvas, (maxAndMin[0] - maxAndMin[1]) / 2 + "", indexStartY + indexH / 2);
    }

    /**
     * 把传入的参数计算成坐标，直接展示到界面上
     * @param input
     * @return 返回里面的StickData的最高价最低价，都是可以直接显示在坐标上的
     */
    public float parseNumber(double input) {

//
//        String s1= NumberManager.getSubString(Double.toString(input),Double.toString(yMin),16);
//        String s2= NumberManager.getSubString(Double.toString(yMax),Double.toString(yMin),16);
//        String s3=NumberManager.getDivString(Double.toString(mainH),s2,16);
//        String s4=NumberManager.getString(s1,s3,16);
//        String s5=NumberManager.getSubString(Double.toString(mainH),s4,16);
//        float f1=Float.parseFloat(s5);
//
//        if(f1==0)
//        {
//            f1=2;
//        }
//        if(f1>=mainH)
//        {
//            f1=mainH-1;
//        }
//        if(f1<0)
//        {
//            f1=mainH-1;
//        }
//       return  f1;
       // float f0=(float) (( input - yMin) / yUnit)-(float) (mainH*0.05);
        float f0=(float) (( input - yMin) / yUnit);
//        if(f0<(mainH*0.05))
//        {
//            f0=(float)(mainH*0.05);
//        }
        float f1=mainH - f0;

        if(f1>mainH)
        {
            f1=mainH;
        }
        return f1;
    }

    /**
     * 根据y轴坐标 计算对应的价格
     * @param y
     * @return
     */
    public float getPrice(float y)
    {
        float f1=mainH - y;

        f1=(float) (f1*yUnit+yMin);
        return  f1;
    }
    /**
     * 画量的均线
     * @param canvas
     * @param max
     */
    private void drawCountSma(Canvas canvas, float max) {
        if(data == null || data.size() == 0) return;
        float[] sma5 = new float[showList.size()];
        float[] sma10 = new float[showList.size()];
        int size = showList.size();

        for (int i = 0; i < showList.size(); i++) {
            if(size > IndexParseUtil.START_SMA5)
                sma5[i] = (float) showList.get(i).getCountSma5();
            if(size > IndexParseUtil.START_SMA10)
                sma10[i] = (float) showList.get(i).getCountSma10();
        }
        DrawUtils.drawLines(canvas, sma5, DEFUALT_WIDTH, indexH - 2, ColorUtil.COLOR_SMA5, max, 0f, false, indexStartY + 2, DEFUALT_WIDTH / 2);
        DrawUtils.drawLines(canvas, sma10, DEFUALT_WIDTH, indexH - 2, ColorUtil.COLOR_SMA10, max, 0f, false, indexStartY + 2, DEFUALT_WIDTH / 2);
    }

    public void setDataAndInvalidate(ArrayList<StickData> data) {
        this.data = data;
        parseData();
        postInvalidate();
    }
    public void upDateAndInvalidate(StickData sd)
    {
        if(data==null||data.size()==0)
        {
             return;
        }
        boolean b=false;
        StickData csd=data.get(data.size()-1);

//        if(csd.getTime()==sd.getTime())
//        {
//            csd.setClose(sd.getClose());
//
//        }
//        else{
            if(lineType==1)
            {
//                long cmin=csd.getTime()/60;
//                long min=sd.getTime()/60;
                if(((sd.getTime())>=(csd.getTime())))
                {
                    long start= LineUtil.countTime(sd.time_jiaoyo,sd.getTime(),lineType);

                    sd.setTime(start);
                    //offset++;
                    data.add(sd);
                    //doIndex();
                    //Log.e("vol_t",csd.getCount()+"");
//                    StickData csd2=data.get(data.size()-2);
//                    Log.e("cmin",csd.getTimeAll2());
//                    Log.e("min",sd.getTimeAll2());
//
//                    Log.e("cmin2",csd.getTime()+"");
//                    Log.e("min2",sd.getTime()+"");
                }
                else
                {

                   b= doUpDate(csd,sd);
                   if(b)
                   {
                       //return;
                   }
                }
            }
            else if(lineType==5)
            {
                long cmin=csd.getTime()/(60*5);
                long min=sd.getTime()/(60*5);
                String s=csd.getTimeMin();
                String s2=sd.getTimeMin();
                if(((sd.getTime())>=(csd.getTime())))
                {
                   long start= LineUtil.countTime(sd.time_jiaoyo,sd.getTime(),lineType);

                   sd.setTime(start);
                  String s3=csd.getTimeMin();
                    String s4=sd.getTimeMin(start);
                    //offset++;
                    data.add(sd);
                }
                else
                {

                    b= doUpDate(csd,sd);
                    if(b)
                    {
                        //return;
                    }
                }
            }
            else if(lineType==10)
            {
                if(((sd.getTime())>=(csd.getTime())))
                {
                    long start= LineUtil.countTime(sd.time_jiaoyo,sd.getTime(),lineType);

                    sd.setTime(start);
                    //offset++;
                    data.add(sd);
                   // doIndex();
                }
                else
                {

                    b= doUpDate(csd,sd);
                    if(b)
                    {
                        //return;
                    }
                }

            }
            else if(lineType==30)
            {
                if(((sd.getTime())>=(csd.getTime())))
                {
                    long start= LineUtil.countTime(sd.time_jiaoyo,sd.getTime(),lineType);

                    sd.setTime(start);
                    //offset++;
                    data.add(sd);
                   // doIndex();
                }
                else
                {

                    b= doUpDate(csd,sd);
                    if(b)
                    {
                        //return;
                    }
                }

            }
            else if(lineType==60)
            {
                if(((sd.getTime())>=(csd.getTime())))
                {
                    long start= LineUtil.countTime(sd.time_jiaoyo,sd.getTime(),lineType);

                    sd.setTime(start);
                    //offset++;
                    data.add(sd);
                   // doIndex();

                }
                else
                {
                    b= doUpDate(csd,sd);
                    if(b)
                    {
                       // return;
                    }
                }
            }
            else if (lineType==24)
            {

//                if(((sd.getTime())>=(csd.getTime())))
//                {
//                    long start= LineUtil.countTime(sd.time_jiaoyo,sd.getTime(),lineType);
//
//                    sd.setTime(start);
//                    //offset++;
//                    data.add(sd);
//
//                }
//                else
//                {
//                    b= doUpDate(csd,sd);
//                    if(b)
//                    {
//                        // return;
//                    }
//                }

                b= doUpDate(csd,sd);
                if(b)
                {
                    // return;
                }
            }
  //      }
        updateZhiBiao();
//        if(offset>0)
//        {
//            return;
//        }
//        if(styleView.getState()>0)
//        {
//            return;
//        }
        if(!b) {
            //praseZhiBiao();
        }

        if(crossView.getVisibility() == VISIBLE) {
            onCrossMove(x, y);
        }
        postInvalidate();

    }
    private  void doIndex()
    {
        if(showList==null)
        {
            return;
        }
        if(showList.size()>=drawCount&&offset>0)
        {
            offset++;
        }
        if(offset==0&&crossView.getVisibility()==VISIBLE)
        {
            offset++;
        }
    }
  private  boolean doUpDate(StickData csd,StickData sd)
  {

      boolean b=false;
      double d = Double.parseDouble(NumberUtil2.getAddString(sd.getCount() + "", csd.getCount() + "", scale_price));
      csd.setCount(d);
      //Log.e("vol",d+"");
      if(NumberUtil2.isDaYu(sd.getClose()+"",csd.getHigh()+"")>0)
      {
          csd.setHigh(sd.getClose());
      }
      if(csd.getLast()!=sd.getClose())
      {
          csd.setLast(sd.getClose());
      }
      if(NumberUtil2.isDaYu(csd.getLow()+"",sd.getClose()+"")>0)
      {
          csd.setLow(sd.getClose());
      }

      if(NumberUtil2.isDaYu(csd.getClose()+"",sd.getClose()+"")==0)
      {
          return true;
      }
      csd.setClose(sd.getClose());
      return  b;
  }
    /**
     * 获取页面一页可展示的数据
     */
    private void getShowList(int offset) {
        if(offset != 0 && data.size() - drawCount - offset < 0) {
            offset = data.size() - drawCount;
        }
        showList = new ArrayList<>();
        showList.addAll(data.subList(data.size() - drawCount - offset, data.size() - offset));
    }

    /**
     * 设置K线类型
     */
    public void setType(int type) {
        styleView=new StyleView(getContext());
        lineType = type;
    }
    @Override
    protected void praseAllZhiBiao()
    {
        if (data == null || data.size() == 0) {
            return;
        }
        IndexParseUtil.initSma(data);
        IndexParseUtil.initEma(data);
        IndexParseUtil.initBOLL(data);

        IndexParseUtil.initMACD(data);
        IndexParseUtil.initKDJ(data);
        IndexParseUtil.initRsi(data);
        IndexParseUtil.initRoc(data);
    }
    private void updateZhiBiao()
    {

        switch (lineIndexType) {
            case kline_MA:
                IndexParseUtil.updateSma(data);
                break;
            case kline_EMA:
                IndexParseUtil.updateEma(data);
                break;
            case kline_boll:
                IndexParseUtil.updateBOLL(data);
                break;
        }
        switch (indexType) {
            case INDEX_MACD:
                IndexParseUtil.updateMACD(data);
                break;
            case INDEX_KDJ:
                IndexParseUtil.updateKDJ(data);
                break;
            case INDEX_RSI:
                IndexParseUtil.updateRsi(data);
                break;
            case INDEX_ROC:
                IndexParseUtil.updateRoc(data);
                break;
        }

    }
    private void praseZhiBiao()
    {
//        IndexParseUtil.initSma(data);
//        IndexParseUtil.initEma(data);
//        IndexParseUtil.initBOLL(data);
//
//        IndexParseUtil.initMACD(data);
//        IndexParseUtil.initKDJ(data);
//        IndexParseUtil.initRsi(data);
//        IndexParseUtil.initRoc(data);
        switch (lineIndexType) {
                    case kline_MA:
                        IndexParseUtil.initSma(data);
                        break;
                    case kline_EMA:
                        IndexParseUtil.initEma(data);
                        break;
                    case kline_boll:
                        IndexParseUtil.initBOLL(data);
                        break;
        }
        switch (indexType) {
            case INDEX_MACD:
                IndexParseUtil.initMACD(data);
                break;
            case INDEX_KDJ:
                IndexParseUtil.initKDJ(data);
                break;
            case INDEX_RSI:
                IndexParseUtil.initRsi(data);
                break;
            case INDEX_ROC:
                IndexParseUtil.initRoc(data);
                break;
        }
        //把暂时不显示的计算，放在线程中去完成，避免阻塞主线程
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                if(data==null)
//                {
//                    return;
//                }
//
//
//
//
//
//
//
//                switch (lineIndexType) {
//                    case kline_MA:
//                        IndexParseUtil.initSma(data);
//                        break;
//                    case kline_EMA:
//                        IndexParseUtil.initEma(data);
//                        break;
//                    case kline_boll:
//                        IndexParseUtil.initBOLL(data);
//                        break;
//                }
//
//            }
//        }).start();
    }
    /**
     * 计算各指标
     */
    private void parseData() {
        offset = 0;
        //根据当前显示的指标类型，优先计算指标
        IndexParseUtil.initSma(this.data);
        switch (indexType) {
            case INDEX_MACD:

                break;
            case INDEX_KDJ:

                break;
        }
        //把暂时不显示的计算，放在线程中去完成，避免阻塞主线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(data==null)
                {
                    return;
                }
                IndexParseUtil.initMACD(data);
                IndexParseUtil.initKDJ(data);
                IndexParseUtil.initRsi(data);
                IndexParseUtil.initRoc(data);
                IndexParseUtil.initEma(data);
                IndexParseUtil.initBOLL(data);


            }
        }).start();
    }

    /**
     * 设置价格精度
     */
   public void setScalePrice(int scale_price)
   {
       this.scale_price=scale_price;
       IndexParseUtil.scale_price=scale_price;
       DrawUtils.setScale(scale_price);
   }
   public  int getScalePrice()
   {
       return scale_price;
   }
    /**
     * 得到指标的值
     * @param y
     * @return
     */
   private String getZhiBiaoValue(float y)
   {

       if(indexType==INDEX_VOL)
       {
           double max = 0;
           for(StickData data : showList)
           {
               max = data.getCount() > max ? data.getCount() : max;
           }
           String s1=DrawUtils.getVolValue(max,y, indexH,indexStartY);
                 s1=  NumberUtil2.getString(s1+"","1",0);
          return s1;

       }
       else if(indexType==INDEX_MACD)
       {
           float[] dif = new float[showList.size()];
           float[] dea = new float[showList.size()];
           float[] macd = new float[showList.size()];
           for (int i = 0; i < showList.size(); i++) {
               //if(data.indexOf(showList.get(i)) > IndexParseUtil.START_DIF)
               dif[i] = (float) showList.get(i).getDif();
               // if(data.indexOf(showList.get(i)) > IndexParseUtil.START_DEA) {
               dea[i] = (float) showList.get(i).getDea();
               macd[i] = (float) showList.get(i).getMacd();
               //}
           }
           float maxAndMin[] = LineUtil.getMaxAndMin(dif, dea, macd);
           String s1=DrawUtils.getMacdValue(maxAndMin[0],maxAndMin[1],y,indexH,indexStartY);
           s1=  NumberUtil2.getString(s1+"","1",scale_price);
           return s1;

       }
       else if(indexType==INDEX_KDJ)
       {
           float[] kl = new float[showList.size()];
           float[] dl = new float[showList.size()];
           float[] jl = new float[showList.size()];
           int size = showList.size();
           for (int i = 0; i < showList.size(); i++) {

               kl[i] = (float) showList.get(i).getK();

               dl[i] = (float) showList.get(i).getD();
               jl[i] = (float) showList.get(i).getJ();

           }
           float maxAndMin[] = LineUtil.getMaxAndMin(kl, dl, jl);
           String s1=DrawUtils.getLineValue(maxAndMin[0],maxAndMin[1],y,indexH,indexStartY);
           s1=  NumberUtil2.getString(s1+"","1",scale_price);
           return s1;
       }
       else if(indexType==INDEX_ROC)
       {
           float[] rocn = new float[showList.size()];
           float[] rocm= new float[showList.size()];
           for (int i = 0; i < showList.size(); i++) {

               rocn[i] = (float) showList.get(i).getRocN();
               rocm[i] = (float) showList.get(i).getRocM();

           }
           float maxAndMin[] = LineUtil.getMaxAndMin(rocn,rocm);
           String s1=DrawUtils.getLineValue(maxAndMin[0],maxAndMin[1],y,indexH,indexStartY);
           s1=  NumberUtil2.getString(s1+"","1",2);
           return s1;
       }
       else if(indexType==INDEX_RSI)
       {
           float[] rsi6 = new float[showList.size()];
           float[] rsi12 = new float[showList.size()];
           float[] rsi24 = new float[showList.size()];

           for (int i = 0; i < showList.size(); i++) {

               rsi6[i] = (float) showList.get(i).getRsi6();


               rsi12[i] = (float) showList.get(i).getRsi12();


               rsi24[i] = (float) showList.get(i).getRsi24();
           }
           float maxAndMin[] = LineUtil.getMaxAndMin(rsi6,rsi12,rsi24);
           String s1=DrawUtils.getLineValue(maxAndMin[0],maxAndMin[1],y,indexH,indexStartY);
           s1=  NumberUtil2.getString(s1+"","1",scale_price);
           return s1;
       }
       double max = 0;
       for(StickData data : showList) {
           max = data.getCount() > max ? data.getCount() : max;
       }
       return  "";
   }
    @Override
    public void onCrossMove(float x, float y) {
        super.onCrossMove(x, y);
        this.x=x;
        this.y=y;
        invalidate();
        if(crossView == null || showList == null) return;
        int position = (int) Math.rint(new Double(x)/ new Double(DEFUALT_WIDTH));
        //如果y轴移动的时间轴  则不画
        if(y>mainH&&y<indexStartY)
        {
            return;
        }
        if(y>mHeight)
        {
            return;
        }
        if(position < showList.size()) {
            //Log.e("move1",position+"");
            if(position<0)
            {
                return;
            }
            StickData data = showList.get(position);
            float xIn = (mWidth / drawCount * position) + (mWidth / candleXDistance / 2);
            //CrossBean bean = new CrossBean(xIn, getY(data.getClose()));
            CrossBean bean = new CrossBean(xIn, y);
           // bean.price = data.getClose() + "";
            bean.price = NumberUtil2.getString(getPrice(y)+"","1",scale_price);
            if(y>indexStartY)
            {
                bean.price=getZhiBiaoValue(y);
            }
            //注意，这个值用来区分，是否画出均线的小圆圈
            bean.y2 = -1;
            if(lineType!=24)
            {
                bean.timeYMD = data.getTime2();
            }
            else
            {
                bean.timeYMD = data.getTime1();
            }

            setIndexTexts(data, bean);
            crossView.drawLine(bean);
            if(crossView.getVisibility() == GONE)
                crossView.setVisibility(VISIBLE);
           //TODO 这里显示该点数据到屏幕
            msgText.setVisibility(VISIBLE);
            //msgText.setText(Html.fromHtml(ColorUtil.getCurPriceInfo(data)));

                msgText.icb.call(data,x,y);


        }
        else if(position ==showList.size())
        {
            if(1==1)
            {
                return;
            }
            Log.e("move2",position+"");
            StickData data = showList.get(position-1);
            float xIn = (mWidth / drawCount * position) + (mWidth / candleXDistance / 2);
            CrossBean bean = new CrossBean(xIn, getY(data.getClose()));
            // bean.price = data.getClose() + "";
            bean.price = NumberUtil2.getString(data.getClose()+"","1",scale_price);
            //注意，这个值用来区分，是否画出均线的小圆圈
            bean.y2 = -1;
            if(lineType!=24)
            {
                bean.timeYMD = data.getTimeAll();
            }
            else
            {
                bean.timeYMD = data.getTime1();
            }

            setIndexTexts(data, bean);
            crossView.drawLine(bean);
            if(crossView.getVisibility() == GONE)
                crossView.setVisibility(VISIBLE);
            //TODO 这里显示该点数据到屏幕
            msgText.setVisibility(VISIBLE);
            //msgText.setText(Html.fromHtml(ColorUtil.getCurPriceInfo(data)));

            msgText.icb.call(data,x,y);
        }
    }



    @Override
    public void onDismiss() {
        msgText.setVisibility(GONE);
        if(showList!=null&&showList.size()>0)
        {
            msgText.icb.cancle(showList.get(showList.size()-1));
        }
        invalidate();
    }

    //获取价格对应的Y轴
    private float getY(double price) {
        return (float) (mainH - (new Float(price) - (float)yMin) / yUnit);
    }

    public void drawZhiBiao(Canvas canvas)
    {
        StickData sd=null;
        double max=0;
        for(int i=0;i<showList.size();i++)
        {
            sd=showList.get(i);
            if(sd.getCount()>max)
            {
                max=sd.getCount();
            }

        }
        CrossBean bean = new CrossBean(0,0);
        int collor=new ColorUtil2(this).getTextColor2();
        int collor7=new ColorUtil2(this).getTextColor7();
        switch (indexType) {
            case INDEX_VOL:
                String vol=NumberUtil2.getString("1",max+"",0);
                bean.indexText = new String[]{"最大成交量:" ,vol};
                bean.indexColor = new int[]{ collor,collor7};

                break;
            case INDEX_MACD:
                String dif=NumberUtil2.getString(data.get(data.size()-1).getDif()+"","1",scale_price);
                String dea=NumberUtil2.getString(data.get(data.size()-1).getDea()+"","1",scale_price);
                String macd=NumberUtil2.getString(data.get(data.size()-1).getMacd()+"","1",scale_price);
                bean.indexText = new String[]{"MACD(12,26,9)","DIF:"+dif,"DEA:"+dea,"MACD:"+macd};
                bean.indexColor = new int[]{collor,ColorUtil.COLOR_SMA5,ColorUtil.COLOR_SMA10,ColorUtil.COLOR_SMA30};
                break;
            case INDEX_KDJ:

                String k=NumberUtil2.getString(data.get(data.size()-1).getK()+"","1",scale_price);
                String d=NumberUtil2.getString(data.get(data.size()-1).getD()+"","1",scale_price);
                String j=NumberUtil2.getString(data.get(data.size()-1).getJ()+"","1",scale_price);
                bean.indexText = new String[]{"KDJ(9,3,3)","K:"+k,"D:"+d,"J:"+j};
                bean.indexColor = new int[]{collor,ColorUtil.COLOR_SMA5,ColorUtil.COLOR_SMA10,ColorUtil.COLOR_SMA30};
                break;
            case INDEX_RSI:
                String s6=NumberUtil2.getString(data.get(data.size()-1).getRsi6()+"","1",2);
                String s12=NumberUtil2.getString(data.get(data.size()-1).getRsi12()+"","1",2);
                String s24=NumberUtil2.getString(data.get(data.size()-1).getRsi24()+"","1",2);
                if(NumberUtil2.isDaYu(s6,StickData.defValue+"")==0)
                {
                    s6="--";
                }
                if(NumberUtil2.isDaYu(s12,StickData.defValue+"")==0)
                {
                    s12="--";
                }
                if(NumberUtil2.isDaYu(s24,StickData.defValue+"")==0)
                {
                    s24="--";
                }
                bean.indexText = new String[]{"RSI(6,12,24)","RSI6:"+s6,"RSI12:"+s12,"RSI24:"+s24};
                bean.indexColor = new int[]{collor,ColorUtil.COLOR_SMA5,ColorUtil.COLOR_SMA10,ColorUtil.COLOR_SMA30};
                break;
            case INDEX_ROC:
                String sn=NumberUtil2.getString(data.get(data.size()-1).getRocN()+"","1",scale_price);
                String sm=NumberUtil2.getString(data.get(data.size()-1).getRocM()+"","1",scale_price);
                if(NumberUtil2.isDaYu(sn,StickData.defValue+"")==0)
                {
                    sn="--";
                }
                if(NumberUtil2.isDaYu(sm,StickData.defValue+"")==0)
                {
                    sm="--";
                }
                bean.indexText = new String[]{"ROC(12,6)","ROC:"+sn,"ROCMA:"+sm};
                bean.indexColor = new int[]{collor,ColorUtil.COLOR_SMA5,ColorUtil.COLOR_SMA10};
                break;
        }

        if(bean.indexText == null || bean.indexColor == null) return;
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setTextSize(26f);
        float x = 10;
        float y = getHeight() * (ChartConstant.MAIN_SCALE + ChartConstant.TIME_SCALE) + 23;
        for(int i = 0;i < bean.indexText.length; i++) {
            p.setColor(bean.indexColor[i]);
            canvas.drawText(bean.indexText[i], x, y, p);
            x += LineUtil.getTextWidth(p, bean.indexText[i]) + 30;
        }
    }
    /**
     * 设置指标左上角的文字
     * @param data
     * @param bean
     */
    private void setIndexTexts(StickData data, CrossBean bean) {
        int collor=new ColorUtil2(this).getTextColor2();
        int collor7=new ColorUtil2(this).getTextColor7();
        switch (indexType) {
            case INDEX_VOL:
                String count=NumberUtil2.getString(data.getCount()+"","1",0);
                bean.indexText = new String[2];
                bean.indexText[0] = "成交量:";
                bean.indexText[1]=count;
                //bean.indexText[1] = "SMA5:" + data.getCountSma5();
               // bean.indexText[2] = "SMA10:" + data.getCountSma10();
                bean.indexColor = new int[] {
                        collor,collor7
                       // data.isRise() ? ColorUtil.COLOR_RED : ColorUtil.COLOR_GREEN,
                       // ColorUtil.COLOR_SMA5,
                        //ColorUtil.COLOR_SMA10
                };
                break;
            case INDEX_ZJ:
                bean.indexText = new String[4];
                bean.indexText[0] = "超大:" + data.getSp();
                bean.indexText[1] = "大:" + data.getBg();
                bean.indexText[2] = "中:" + data.getMd();
                bean.indexText[3] = "小:" + data.getSm();
                bean.indexColor = new int[]{
                        ColorUtil.COLOR_ZJ_SUPER,
                        ColorUtil.COLOR_ZJ_BIG,
                        ColorUtil.COLOR_ZJ_MIDDLE,
                        ColorUtil.COLOR_ZJ_SMALL
                };
                break;
            case INDEX_MACD:
                String dif=NumberUtil2.getString(data.getDif()+"","1",scale_price);
                String dea=NumberUtil2.getString(data.getDea()+"","1",scale_price);
                String macd=NumberUtil2.getString(data.getMacd()+"","1",scale_price);
                bean.indexText = new String[4];
                bean.indexText[0] = "MACD(12,26,9)";
                bean.indexText[1] = "DIF：" + dif;
                bean.indexText[2] = "DEA：" + dea;
                bean.indexText[3] = "MACD：" + macd;
                bean.indexColor = new int[] {
                        collor,
                        ColorUtil.COLOR_SMA5,
                        ColorUtil.COLOR_SMA10,
                        ColorUtil.COLOR_SMA30
                };
                break;
            case INDEX_KDJ:
                String k=NumberUtil2.getString(data.getK()+"","1",scale_price);
                String d=NumberUtil2.getString(data.getD()+"","1",scale_price);
                String j=NumberUtil2.getString(data.getJ()+"","1",scale_price);
                bean.indexText = new String[4];
                bean.indexText[0] = "KDJ(9,3,3)";
                bean.indexText[1] = "K：" + k;
                bean.indexText[2] = "D：" + d;
                bean.indexText[3] = "J：" + j;
                bean.indexColor = new int[] {
                        collor,
                        ColorUtil.COLOR_SMA5,
                        ColorUtil.COLOR_SMA10,
                        ColorUtil.COLOR_SMA30
                };
                break;
            case INDEX_RSI:
                String s6=NumberUtil2.getString(data.getRsi6()+"","1",2);
                String s12=NumberUtil2.getString(data.getRsi12()+"","1",2);
                String s24=NumberUtil2.getString(data.getRsi24()+"","1",2);
                if(NumberUtil2.isDaYu(s6,StickData.defValue+"")==0)
                {
                      s6="--";
                }
                if(NumberUtil2.isDaYu(s12,StickData.defValue+"")==0)
                {
                    s12="--";
                }
                if(NumberUtil2.isDaYu(s24,StickData.defValue+"")==0)
                {
                    s24="--";
                }
                bean.indexText = new String[]{"RSI(6,12,24)","RSI6:"+s6,"RSI12:"+s12,"RSI24:"+s24};
                bean.indexColor = new int[]{ collor,ColorUtil.COLOR_SMA5,ColorUtil.COLOR_SMA10,ColorUtil.COLOR_SMA30};
                break;
            case INDEX_ROC:
                String sn=NumberUtil2.getString(data.getRocN()+"","1",scale_price);
                String sm=NumberUtil2.getString(data.getRocM()+"","1",scale_price);

                if(NumberUtil2.isDaYu(sn,StickData.defValue+"")==0)
                {
                    sn="--";
                }
                if(NumberUtil2.isDaYu(sm,StickData.defValue+"")==0)
                {
                    sm="--";
                }
                bean.indexText = new String[]{"ROC(12,6)","ROC:"+sn,"ROCMA:"+sm};
                bean.indexColor = new int[]{ collor,ColorUtil.COLOR_SMA5,ColorUtil.COLOR_SMA10};
                break;
        }

    }


    public Bitmap splitYuan(float x,float y)
    {

        getDrawingCache();
        Bitmap bitmap=getDrawingCache();
        float width=bitmap.getWidth();
        float height=bitmap.getHeight();
        int startx=(int)(x-50);
        int starty=(int)(y-50);

        if(startx<0)
        {
            startx=0;
        }
        if(starty<0)
        {
            starty=0;
        }

        Bitmap bitmap2=Bitmap.createBitmap(bitmap,startx,starty,60,60);


        return bitmap2;

    }
}
